<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>订单管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="<%=basePath %>web/js/jquery.min.js"></script>
    <script src="<%=basePath %>web/js/jquery.easyui.min.js"></script>
    <script src="<%=basePath %>web/js/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" href="<%=basePath %>web/themes/icon.css">
    <link rel="stylesheet" href="<%=basePath %>web/themes/default/easyui.css">
    <link rel="stylesheet" href="<%=basePath %>web/css/wu.css">

  </head>
  
 <script>
        var  selectIndex = -1;
        function hideTable(){
            $("#searchdiv").toggle();
        }
          function purchaseOrderUpdate(code,num) {
          	var path=$("#path").val();
            parent.addTabs("修改订单",path+"PurchaseOrderBill/getByCodeUpdate?code="+code+"&num="+num+"");
        }
          function purchaseOrderShow(code) {
          	var path=$("#path").val();
            parent.addTabs("查看订单",path+"PurchaseOrderBill/getByCode?code="+code);
        }
        $(function(){
       $("#search_btn").click(function(){
	       var form=$("#form1");
  		 $("#productList").datagrid('load',serializeForm(form));
       });
        $("#reset_btn").click(function(){
             	$("#form1").form('reset');
             	$("#productList").datagrid('load',serializeForm);
             });
			//将表单序列化
			function serializeForm(form){
				var obj={};
			$.each(form.serializeArray(),function(index){
				  if(obj[this['name']]){
					obj[this['name']]=obj[this['name']]+','+this['value']
				 }else{
				   obj[this['name']]=this['value'];
				 }
			});
			return obj;
		}   	
			var path=$("#path").val();
            $("#productList").datagrid({
                url:path+'PurchaseOrderBill/getall',
                rownumbers:true,
                checkOnSelect:false,
                pageNumber:1,
                pageSize:5,
                pageList:[5,10,15,20],
                columns:[[
                    {field:'ck',checkbox:true},
                    {field:'code',title:'订单编号',width:150,
    					formatter:function(value,row,index){
                                  return  "<a href='javascript:void(0)' onclick=\"purchaseOrderShow('"+row.code+"')\" style='color:blue;text-decoration:underline'>"+row.code+"</a>"
                        }
                    },
                    {field:'orderTime',title:'订单日期',width:150},
                    {field:'supplierName',title:'供应商名',width:180},
                    {field:'money',title:'金额',width:100,
                        formatter:function(value,row,index){
                            return "￥"+row.money;
                        }
                    },
                    {field:'linkman',title:'联系人',width:90},
                    {field:'phone',title:'联系方式',width:100},
                    {field:'checkStatus',title:'审核状态',width:80,
                        formatter:function(value,row,index){
                            if(row.checkStatus==1){
                              return "未审核";
                            }
                            return "已审核";
                        },
                        styler: function(value,row,index){
                         if(row.checkStatus==1){
                              return 'color:red;';
                            }
						}
                    },
                    {field:'name',title:'操作员',width:80},
                    {field:'opt',title:'操作',width:169,formatter:function (value,row,index) {
                    					
                            var context="<a href='javascript:void(0)' onclick=\"purchaseOrderUpdate('"+row.code+"','"+row.checkStatus+"')\" style='color:blue;text-decoration:underline'>修改</a>&nbsp;&nbsp;" +
                                "<a href='javascript:void(0)'style='color:blue;text-decoration:underline;'  onclick=\"onedel('"+row.code+"','"+row.checkStatus+"')\">删除</a>&nbsp;"
                            return context;
                        }}

                ]],
                toolbar:'#wu_toolbar',
                 onClickRow:function(index,row){
                	$(this).datagrid("unselectRow",index);
                },
                pagination:true ,//向后台传递参数page 和rows
                  onDblClickRow : function(index, row) {
                $("#show").show();
                $("#detailed").html("<h3>单据标号为："+row.code+" 的明细如下所列<h3>");
             	 $("#partList").datagrid({
                url:path+'part/getByCode',
                queryParams:{code:row.code},
                checkOnSelect:true,
                rownumbers:true,
                columns:[[
                    {field:'code',title:'询价编号',width:130},
                    {field:'partName',title:'配件名称',width:240},
                    {field:'partBrand',title:'配件品牌',width:110},
                    {field:'partNo',title:'配件型号',width:90},
                    {field:'amount',title:'数量',width:90
                    	
                    },
                    {field:'avg',title:'单价',width:90, 
                        formatter:function(value,row,index){
                        	if(row.sellPrice==null){
                        		return "";
                        	}
                            return "￥"+row.price/row.amount;
                        }
                    },
                    {field:'price',title:'总金额',width:100,
                         formatter:function(value,row,index){
                            return "￥"+row.price;
                        }
                    },
                    {field:'pd_memo',title:'备注',width:180},
                    {field:'sellPrice',title:'上次价格',width:97,
                    formatter:function(value,row,index){
                   			 if(row.sellPrice==null){
                        		return "";
                        	}
                            return "￥"+row.sellPrice;
                        }
                    }

                ]],
                  onClickRow:function(index,data){
                	$(this).datagrid('unselectRow',index);
                },
                   onLoadSuccess:function(){
	                $("#partList").datagrid('appendRow',{
	                	  'partBrand':"合计",
	                	  'amount':total('amount'),
	                	  'price':total('price')
	                   });
                    }
                  });
                }
            })
        })
	   function onedel(code,flag){
	   		var path=$("#path").val();
		   	$.messager.confirm('确认提醒','您确认想要删除记录吗？',function(r){    
	   			if (r){    
	   				 $.ajax({
							type:"get",//请求类型
							url:path+"PurchaseOrderBill/delBill_Detail", 
							dataType:"json",//ajax接口（请求url）返回的数据类型
							data:{'code':code,'flag':flag},
							success:function(data){//data：返回数据（json对象）
								if(data.shenhe=="n"){
									 $.messager.alert('我的消息','编号'+code+'处于已审核状态,删除失败!','error');
								}else{
								 if(data.flag=="y"){
									     $.messager.alert('我的消息','编号'+code+'删除成功! --- 并删除其名细'+data.zi+'条记录','info');
									     $('#productList').datagrid('reload');
									     $("#show").hide();
								  }else{
									   	 $.messager.alert('我的消息','编号'+code+'删除失败!','error');
								  }
								}
							}
					   });
		   			}  
			 });  
	   }   
       function total(col){
        	var rows=$("#partList").datagrid('getRows');
        	var total=0;
        	for(var i=0;i<rows.length;i++){
        		total+=parseFloat(rows[i][col]);
        	}
        	return total;
        }
        function detail(id) {
            $.messager.alert("信息","查看"+id+"产品详情");
        }
        function addProduct() {
            parent.addTabs("添加订单","goods/addOrderBill.jsp");
        }
        function del() {
            var path=$("#path").val();
            var flag=false;
            var rows=$("#productList").datagrid('getSelections');
             var jsonrows=JSON.stringify(rows);
            if(rows.length==0){
              $.messager.alert('我的消息','请选择要删除的目标!','warning');
                return false;
            }
            for(var i=0;i<rows.length;i++){
            	if(rows[i].checkStatus==2){
            	    $.messager.alert('我的消息','所选数据中有已审核!','error');
					return false;            	
            	}
            }
             $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                       $.ajax({
							type:"get",//请求类型
							url:path+"PurchaseOrderBill/deleteMultiple", 
							dataType:"json",//ajax接口（请求url）返回的数据类型
							data:{'data':jsonrows},
							success:function(data){//data：返回数据（json对象）
							 	  if(data.flag=="y"){
									     $.messager.alert('我的消息','数量:'+data.size+'条记录删除成功! --- 并删除其名细'+data.sum+'条记录','info');
									     $('#productList').datagrid('reload');
									     $("#show").hide();
								  }else{
									   	 $.messager.alert('我的消息','删除失败!','error');
								  }
							}
					 }); 
                }
            }); 

        }
        function reload(){
        	location.reload();
        }
    </script>

</head>
<body>
<div id="wu_toolbar">
    <div class="wu_toolbar_button">
        <a  href="javascript:void(0)" onclick="hideTable()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" plain="true">查询</a>|
        <a  href="javascript:void(0)" onclick="addProduct()" class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true">添加</a>|
        <a  href="javascript:void(0)" onclick="del()" class="easyui-linkbutton" data-options="iconCls:'icon-delete-self'" plain="true">删除</a>|
        <a  href="javascript:void(0)" onclick="reload()" class="easyui-linkbutton" data-options="iconCls:'icon-page-refresh'" plain="true">刷新</a>|
        <a  href="javascript:void(0)" onclick="reload()" class="easyui-linkbutton" data-options="iconCls:'icon-communication'" plain="true">导出excal</a>
    </div>
    <div id="searchdiv" style="display: block;width: 100%;">
        <form name="form1" id="form1">

            <table width="100%" border="0" cellspacing="0" style="background-color:#c9dfff;font-size: 14px " >
                <tbody><tr>
                    <td width="70"><b>检索条件:</b></td>
                    <td width="70" align="right">询价编号:</td>
                    <td width="150" align="left"><input id="code" name="code" class="easyui-textbox" type="text" id="findcode" value="" maxlength="100"></td>
                    <td width="70" align="right">开始日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="orderTime" type="text" id="orderTime" value="" maxlength="30" class="easyui-datetimebox" ligeruiid="findstartDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">结束日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="endDate" type="text" id="endDate" value="" maxlength="30" class="easyui-datetimebox" ligeruiid="findendDate" ><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">供应商名:</td>
                    <td width="150" align="left"><input name="supplierName" class="easyui-textbox" type="text" id="supplierName" value="" maxlength="100"></td>
                    <td width="72">
                        <a id="search_btn"  href="javascript:void(0)" class="easyui-linkbutton"  data-options="iconCls:'icon-search'">搜索</a>
                    </td>
                    <td width="72">
                        <a id="reset_btn" href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'">重置</a>
                    </td>
                    <td width="0"></td>


                </tr>
                </tbody>
            </table>
            <input type="hidden" id="menuCode" value="100001001">
            <input type="hidden" id="path" value="<%=basePath%>">
        </form>
    </div>
</div>
<div id="productList" class="easyui-datagrid">


</div>
<div id="show" style="margin-top:10px;display:none">
  <div id="detailed" style="margin-bottom:10px">
    
  </div>
  <div id="partList" class="easyui-datagrid">
     
  </div>
</div>
</body>
</html>
